package com.zyd.blog.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.blog.business.entity.ArticleFavor;
import com.zyd.blog.business.entity.CaredUser;
import com.zyd.blog.business.service.BizArticleFavorService;
import com.zyd.blog.business.service.BizCareUserService;
import com.zyd.blog.business.vo.ArticleFavorConditionVO;
import com.zyd.blog.business.vo.CaredUserConditionVO;
import com.zyd.blog.persistence.beans.BizArticleFavor;
import com.zyd.blog.persistence.beans.BizArticleTags;
import com.zyd.blog.persistence.beans.BizCaredUser;
import com.zyd.blog.persistence.mapper.BizArticleFavorMapper;
import com.zyd.blog.persistence.mapper.BizCaredUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 文章浏览记录
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @website https://www.zhyd.me
 * @date 2018/4/16 16:26
 * @since 1.0
 */
@Service
public class BizCaredUserServiceImpl implements BizCareUserService {

    @Autowired
    private BizCaredUserMapper bizCaredUserMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CaredUser insert(CaredUser entity) {
        Assert.notNull(entity, "CaredUser不可为空！");
        entity.setUpdateTime(new Date());
        entity.setCreateTime(new Date());
        bizCaredUserMapper.insertSelective(entity.getBizCaredUser());
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CaredUser delete(CaredUser entity) {
        Assert.notNull(entity, "CaredUser不可为空！");
        entity.setUpdateTime(new Date());
        entity.setCreateTime(new Date());

        Example example = new Example(BizCaredUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("caredUserId", entity.getCaredUserId());
        criteria.andEqualTo("userId", entity.getUserId());
        bizCaredUserMapper.deleteByExample(example);
        return entity;
    }

    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<CaredUser> findPageBreakByCondition(CaredUserConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<BizCaredUser> list = bizCaredUserMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        PageInfo bean = new PageInfo<BizCaredUser>(list);
        return bean;
    }
}
